package com.cooba.activitys;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;

import com.cooba.R;
import com.cooba.adapter.DrawOutRecordAdapter;
import com.cooba.adapter.RedWalletDetailAdapter;
import com.cooba.bean.DrawOutRecordBean;
import com.cooba.bean.RedWalletDetailBean;
import com.cooba.pullTorefresh.PullToRefreshBase;
import com.cooba.pullTorefresh.PullToRefreshListView;
import com.cooba.utils.ServerURL;
import com.cooba.utils.SharedPreferencesUtil;
import com.cooba.utils.Utils;
import com.cooba.views.loadview.LoadViewHelper;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import java.util.ArrayList;

public class RedWalletDetailListActivity extends AppCompatActivity implements View.OnClickListener{
    private ImageButton red_wallet_detail_back_btn;
    private PullToRefreshListView rListView;
    private HttpUtils httpUtils;
    private RequestParams params;
    private String next_key = "0";
    private ArrayList<RedWalletDetailBean.DbDetailInfo> mRedWalletDetailList;
    private RedWalletDetailAdapter mAdapter;
    private boolean mIsFirstLoad=true;
    private LoadViewHelper helper;
    private RedWalletDetailBean redwalletdetailbean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_red_wallet_detail_list);
        initView();
        getRedWalletDetail(true);
    }
    private void initView() {
        mRedWalletDetailList = new ArrayList<>();
        red_wallet_detail_back_btn = (ImageButton) findViewById(R.id.red_wallet_detail_back_ib);
        red_wallet_detail_back_btn.setOnClickListener(this);
        initPullTorefresh();
        helper= Utils.initLoadViewHelper(rListView);
    }
    private void initPullTorefresh() {
        rListView = (PullToRefreshListView) findViewById(R.id.red_wallet_detail_lv);
        rListView.setPullRefreshEnabled(false);
        rListView.setPullLoadEnabled(true);
        rListView.setScrollLoadEnabled(true); //设置滚动加载可用
        mAdapter = new RedWalletDetailAdapter(this, mRedWalletDetailList);
        rListView.getRefreshableView().setAdapter(mAdapter);
        rListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {//下拉刷新

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {//上拉加载
                getRedWalletDetail(false);
            }
        });
    }

    private void getRedWalletDetail(final boolean isFirst) {
        if (mIsFirstLoad) {
            helper.showLoading("正在加载...");
        }
        httpUtils = new HttpUtils();
        params = new RequestParams();
        String login_id = SharedPreferencesUtil.getData(this, "login_id", "");
        params.addBodyParameter("user_id", login_id);
        params.addBodyParameter("next_key", next_key);
        params.addBodyParameter("page_size", "10");
        String urlredwalletdetailrecord = ServerURL.httpUrl + ServerURL.redwalletdetailrecord;
        httpUtils.send(HttpRequest.HttpMethod.POST, urlredwalletdetailrecord, params, new RequestCallBack<String>() {
            @Override
            public void onFailure(HttpException arg0, String arg1) {
                Log.e("wei", "获取红包详情失败");
                Toast.makeText(RedWalletDetailListActivity.this, "无网络!", Toast.LENGTH_SHORT).show();
                rListView.setHasMoreData(false);
                rListView.onPullDownRefreshComplete();
                rListView.onPullUpRefreshComplete();
            }

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Log.e("wei", "获取红包详情成功" + responseInfo.result);
                try {
                    redwalletdetailbean = new Gson().fromJson(responseInfo.result, RedWalletDetailBean.class);
                    if (redwalletdetailbean.getDbDetailList() != null && redwalletdetailbean.getDbDetailList().size() > 0) {
                        if (isFirst) {
                            mAdapter.setList(redwalletdetailbean.getDbDetailList());
                        } else {
                            mAdapter.addMoreData(redwalletdetailbean.getDbDetailList());
                        }
                        if (redwalletdetailbean.getNext_key() == null) {
                            next_key = String.valueOf(redwalletdetailbean.getDbDetailList().size() + Integer.parseInt(next_key));
                        } else {
                            next_key = redwalletdetailbean.getNext_key();
                        }
                        if (redwalletdetailbean.getDbDetailList().size() < 10) {
                            rListView.setHasMoreData(false);
                        }
                        mIsFirstLoad = false;
                        helper.restore();
                    } else {
                        if (isFirst) {
                            helper.showEmpty("暂时没有数据", "重新加载", false, null);
                        } else {
                            Toast.makeText(RedWalletDetailListActivity.this, "没有更多数据啦", Toast.LENGTH_SHORT).show();
                        }
                    }
                } catch (JsonSyntaxException e) {
                    e.printStackTrace();
                } finally {

                    rListView.onPullDownRefreshComplete();
                    rListView.onPullUpRefreshComplete();

                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.red_wallet_detail_back_ib:
                finish();
                break;
            default:
                break;
        }
    }
}
